﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace SecurityDev
{
    public partial class PersonDetail : Form
    {
        public PersonDetail(decimal person_seq)
        {
            OutpostDS.personRow personR = DS.outpost.person.FindByperson_seq(person_seq);

            InitializeComponent();
            this.Text = label2.Text = personR.person_name + " " + personR.person_lastname;

            label1.Text = personR.person_seq.ToString();
            label2.Text = personR.person_name;
            label3.Text = personR.person_lastname;
            label4.Text = personR.person_phone;
            label5.Text = personR.person_email;
            label6.Text = personR.person_adress;
            label8.Text = personR.person_is_customer.ToString();

            LoadImage(pictureBox, personR.person_photo, false);

            decimal[] objSeq_inObjPer = (from op in DS.outpost.object_person
                                         where op.person_seq == person_seq
                                         select op.obj_seq).ToArray();

            var objsStr = from o in DS.outpost._object
                          where CheckObjSeq_inArray(o.obj_seq, objSeq_inObjPer)
                          select string.Format("Id: {0}; Name: {1}", o.obj_seq.ToString(), o.obj_name);

            foreach (var item in objsStr)
            {
                objectsListBox.Items.Add((object)item);
            }
        }

        bool CheckObjSeq_inArray(decimal objSeq, decimal[] array)
        {
            foreach (var item in array)
            {
                if (objSeq == item)
                {
                    return true;
                }
            }
            return false;
        }

        private void objectsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (objectsListBox.SelectedIndex != -1)
            {
                decimal obj_seq = Convert.ToDecimal(((string)objectsListBox.SelectedItem).Split(';')[0].Substring(4));
                (new ObjectDetail(obj_seq)).ShowDialog();
            }
            objectsListBox.ClearSelected();
        }

        private void LoadImage(PictureBox p1, string path, bool type)
        {
            // if (type == true) - open in Browser - else only on Form
            if (type == true)
            {
                ProcessStartInfo sInfo = new ProcessStartInfo(path);
                Process.Start(sInfo);
            }
            try
            {
                p1.Load(path);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void PersonDetail_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape)
            {
                Close();
            }
        }
    }
}
